###############################################                                     
# Cause of Effect? Turnout in Hispanic Majority-Minority Districts
#  - John A. Henderson, Jasjeet S. Sekhon, and Rocio Titiunik
#  - Forthcoming in Political Analysis
#  - Replication file for <Figure 1c>
#  - April 14, 2016
###############################################

options(width=150)
rm(list=ls())   

path='/local/'  
source(paste(path,'replicationPA/funs/headers.R',sep=''))
   
# LOAD BASELINE AND MATCHED DATA
load(paste(path,"replicationPA/Figure1/CA2010_final_data_merged.Rdata",sep=''))   
set.seed(57459)

Tr=as.numeric(as.character(redisData2010$Tr))
Tr[which(as.numeric(as.character(redisData2010$hisp02))==1)]=NA

x=as.numeric(as.character(redisData2010$hispanic18))/as.numeric(as.character(redisData2010$total18))
                                              
yhispReg=as.numeric(as.character(redisData2010$hispReg))/as.numeric(as.character(redisData2010$hispanic18))
yhispVot=as.numeric(as.character(redisData2010$hispVot))/as.numeric(as.character(redisData2010$hispanic18))
yhispVotReg=as.numeric(as.character(redisData2010$hispVot))/as.numeric(as.character(redisData2010$hispReg))
        
yhispReg[which(as.numeric(as.character(redisData2010$hispanic18))==0)]=NA
yhispReg[which(yhispReg>1)]=NA
yhispVot[which(as.numeric(as.character(redisData2010$hispanic18))==0)]=NA
yhispVot[which(yhispVot>1)]=NA
# over registration
yhispVotReg[which(as.numeric(as.character(redisData2010$hispReg))==0)]=NA
yhispVotReg[which(yhispVotReg>1)]=NA                              

             
set.seed(1005)                   
indxna=which(!is.na(Tr) & !is.na(x))   
  
use.matching=F
if(use.matching==T){
	mout=Match(Tr=Tr[indxna],X=x[indxna],ties=F) 
} else{
	load(paste(path,'replicationPA/Figure1/mout2010-CA.Rdata',sep='')) 	
}
         
tr_mout=Tr[indxna][c(mout$index.treated,mout$index.control)]
yhispReg_mout=yhispReg[indxna][c(mout$index.treated,mout$index.control)]
yhispVot_mout=yhispVot[indxna][c(mout$index.treated,mout$index.control)]      
yhispVotReg_mout=yhispVotReg[indxna][c(mout$index.treated,mout$index.control)]  
          
pdf(file=paste(path,"replicationPA/Figure1/conditionRegBias2010.pdf",sep=''))
qqplot(yhispReg_mout[which(tr_mout==1)],yhispReg_mout[which(tr_mout==0)],ylim=c(0,1),xlim=c(0,1),
  	xlab="Blocks to be moved from Non-Hispanic White incumbent to Hispanic incumbent",
  	ylab="Blocks with same Non-Hispanic White incumbent before and after")
 	abline(a=0,b=1,col='red')   
	m1=mean(yhispReg_mout[which(tr_mout==1)],na.rm=T)
	m0=mean(yhispReg_mout[which(tr_mout==0)],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
    rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()      

png(file=paste(path,"replicationPA/Figure1/conditionRegBias2010.png",sep=''),res=65)
	qqplot(yhispReg_mout[which(tr_mout==1)],yhispReg_mout[which(tr_mout==0)],ylim=c(0,1),xlim=c(0,1),
  	xlab="Blocks to be moved from Non-Hispanic White incumbent to Hispanic incumbent",
  	ylab="Blocks with same Non-Hispanic White incumbent before and after")
 	abline(a=0,b=1,col='red')   
	m1=mean(yhispReg_mout[which(tr_mout==1)],na.rm=T)
	m0=mean(yhispReg_mout[which(tr_mout==0)],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
    rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()          

pdf(file=paste(path,"replicationPA/Figure1/conditionVotBias2010.pdf",sep=''))
	qqplot(yhispVot_mout[which(tr_mout==1)],yhispVot_mout[which(tr_mout==0)],ylim=c(0,1),xlim=c(0,1),
  	xlab="Blocks to be moved from Non-Hispanic White incumbent to Hispanic incumbent",
  	ylab="Blocks with same Non-Hispanic White incumbent before and after")
 	abline(a=0,b=1,col='red')   
	m1=mean(yhispVot_mout[which(tr_mout==1)],na.rm=T)
	m0=mean(yhispVot_mout[which(tr_mout==0)],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
    rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()   

png(file=paste(path,"replicationPA/Figure1/conditionVotBias2010.png",sep=''),res=65)
	qqplot(yhispVot_mout[which(tr_mout==1)],yhispVot_mout[which(tr_mout==0)],ylim=c(0,1),xlim=c(0,1),
  	xlab="Blocks to be moved from Non-Hispanic White incumbent to Hispanic incumbent",
  	ylab="Blocks with same Non-Hispanic White incumbent before and after")
 	abline(a=0,b=1,col='red')   
	m1=mean(yhispVot_mout[which(tr_mout==1)],na.rm=T)
	m0=mean(yhispVot_mout[which(tr_mout==0)],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
    rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off() 

pdf(file=paste(path,"replicationPA/Figure1/conditionVotRegBias2010.pdf",sep=''))
	qqplot(yhispVotReg_mout[which(tr_mout==1)],yhispVotReg_mout[which(tr_mout==0)],ylim=c(0,1),xlim=c(0,1),
  	xlab="Blocks to be moved from Non-Hispanic White incumbent to Hispanic incumbent",
  	ylab="Blocks with same Non-Hispanic White incumbent before and after")
 	abline(a=0,b=1,col='red')   
	m1=mean(yhispVotReg_mout[which(tr_mout==1)],na.rm=T)
	m0=mean(yhispVotReg_mout[which(tr_mout==0)],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
    rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()
   
png(file=paste(path,"replicationPA/Figure1/conditionVotRegBias2010.png",sep=''),res=65)
	qqplot(yhispVotReg_mout[which(tr_mout==1)],yhispVotReg_mout[which(tr_mout==0)],ylim=c(0,1),xlim=c(0,1),
  	xlab="Blocks to be moved from Non-Hispanic White incumbent to Hispanic incumbent",
  	ylab="Blocks with same Non-Hispanic White incumbent before and after")
 	abline(a=0,b=1,col='red')   
	m1=mean(yhispVotReg_mout[which(tr_mout==1)],na.rm=T)
	m0=mean(yhispVotReg_mout[which(tr_mout==0)],na.rm=T)   
	legend(cex=1.25,x=c(0,0),y=c(.9,.7),pch=c('-'),
	legend=c(paste('Mean Treated: ' ,round(m1,digits=2) ,sep=''),
			 paste('Mean Control: ' ,round(m0,digits=2) ,sep='')),box.col='white')   
    rect(xleft=0,xright=.425,ytop=.9,ybottom=.745)		   			
dev.off()
    
# End Figure1c.R